home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Apps / AudioApps / Resound / FileController.m < prev    next >
Text File  |  1992-12-20  |  14KB  |  660 lines

  1.  
  2.  
  3. #import "FileController.h"
  4. #import "Imports.h"
  5.  
  6.  
  7. @implementation FileController
  8.  
  9. -init
  10. {    
  11.     [super init];
  12.     CurrentPanel=NULL;
  13.     DoClose=YES;
  14.     PanelCancelled=NO;
  15.     DoQuit=NO;
  16.     Quitting=NO;
  17.     return self;
  18. }
  19.  
  20. - New:sender
  21. {
  22.     [self NewRecordedSound:NULL];
  23.     return self;
  24. }
  25.  
  26. - NewRecordedSound: ThisSound
  27. {
  28.     /* Stolen right out of the NeXT Concepts 2.0, p. 9-33 */
  29.     
  30.     id theWindow, theScrollView, theSoundView, theSound;
  31.     NXRect aRect, contentRect;
  32.     
  33.     [ThePreferencesManager SoundComingIn];
  34.     [TheSoundTable NewWindowPos];
  35.     NXSetRect(&aRect,[TheSoundTable WindowPosX],
  36.                     [TheSoundTable WindowPosY],600.0,250.0);
  37.     theWindow=[Window newContent:&aRect
  38.                             style:NX_TITLEDSTYLE
  39.                             backing:NX_BUFFERED
  40.                             buttonMask:NX_ALLBUTTONS
  41.                             defer:NO];
  42.     [theWindow setBackgroundGray:NX_WHITE];
  43.     theScrollView=[ScrollView newFrame:&aRect];
  44.     [theScrollView setVertScrollerRequired:NO];
  45.     [theScrollView setHorizScrollerRequired:YES];
  46.     
  47.     [theWindow setContentView:theScrollView];
  48.     contentRect=aRect;
  49.     [ScrollView getContentSize:&(contentRect.size)
  50.             forFrameSize:&(aRect.size)
  51.             horizScroller:YES
  52.             vertScroller:NO
  53.             borderType:NX_NOBORDER];
  54.     theSoundView = [UpdateSoundView newFrame:&contentRect];
  55.         // my custom sound view.
  56.     [theSoundView HeresTheEditController: TheEditController : [ThePreferencesManager ContinuousView]];
  57.     [theSoundView notifyAncestorWhenFrameChanged:YES];
  58.     
  59.     if (ThisSound==NULL)
  60.         {
  61.         theSound = [[Sound alloc] init];
  62.         }
  63.     else
  64.         {
  65.         theSound=ThisSound;
  66.         }
  67.         
  68.     [theSoundView setSound:theSound];
  69.     [theSoundView setDisplayMode:[ThePreferencesManager MinMaxDisplay]];    [theScrollView setDocView:theSoundView];
  70.  
  71.     [[theScrollView superview] setAutoresizeSubviews:YES];
  72.     [[theScrollView superview] setAutosizing:NX_HEIGHTSIZABLE|NX_WIDTHSIZABLE];
  73.     [TheSoundTable NewItem:theSound:theSoundView:theWindow:"UNTITLED":YES];
  74.     
  75.     [theWindow setDelegate:self];
  76.     [theWindow setTitle:"UNTITLED"];
  77.     [theWindow setMiniwindowIcon: "SoundIcon"];
  78.     [theWindow makeKeyAndOrderFront:self];
  79.     [theWindow display];
  80.     [theSoundView setDelegate:TheSoundManager];
  81.      [theSoundView setContinuous: [ThePreferencesManager ContinuousSelection]];        // For Continuous Sample Update
  82.     [theSound setDelegate:TheSoundManager];
  83.     [self TurnOnMenu:self];
  84.     if (ThisSound==theSound) [self SoundChanged: ThisSound];    return self;
  85. }
  86.  
  87. - Open:sender
  88. {
  89.     id theWindow, theScrollView, theSoundView, theSound;
  90.     NXRect aRect, contentRect;
  91.     id openpanel;
  92.     char nameoffile[MAXSTRINGLENGTH];
  93.     char const*filetype[2]={0,0};
  94.     
  95.     PanelCancelled=YES;
  96.     filetype[0]="snd";
  97.     openpanel=[OpenPanel new];
  98.     [openpanel setDelegate:self];
  99.     [openpanel allowMultipleFiles:NO];
  100.     CurrentPanel=openpanel;
  101.     [openpanel runModalForTypes:filetype];
  102.     if (!PanelCancelled)
  103.     {
  104.         strcpy (nameoffile, [openpanel filename]);
  105.             
  106.         /* Stolen right out of the NeXT Concepts 2.0, p. 9-33 */
  107.     
  108.         [ThePreferencesManager SoundComingIn];
  109.         [TheSoundTable NewWindowPos];
  110.         NXSetRect(&aRect,[TheSoundTable WindowPosX],
  111.                     [TheSoundTable WindowPosY],600.0,250.0);
  112.         theWindow=[Window newContent:&aRect
  113.                                 style:NX_TITLEDSTYLE
  114.                                 backing:NX_BUFFERED
  115.                                 buttonMask:NX_ALLBUTTONS
  116.                                 defer:NO];
  117.         [theWindow setBackgroundGray:NX_WHITE];
  118.         
  119.         theScrollView=[ScrollView newFrame:&aRect];
  120.         [theScrollView setVertScrollerRequired:NO];
  121.         [theScrollView setHorizScrollerRequired:YES];
  122.         
  123.         [theWindow setContentView:theScrollView];
  124.             
  125.         contentRect=aRect;
  126.         [ScrollView getContentSize:&(contentRect.size)
  127.                 forFrameSize:&(aRect.size)
  128.                 horizScroller:YES
  129.                 vertScroller:NO
  130.                 borderType:NX_NOBORDER];
  131.     
  132.         theSoundView = [UpdateSoundView newFrame:&contentRect];
  133.             // my custom sound view.
  134.         [theSoundView HeresTheEditController: TheEditController : [ThePreferencesManager ContinuousView]];
  135.         [theSoundView notifyAncestorWhenFrameChanged:YES];
  136.         theSound = [[Sound alloc] initFromSoundfile:nameoffile];
  137.  
  138.         [theSoundView setSound:theSound];
  139.         [theSoundView setDisplayMode:[ThePreferencesManager MinMaxDisplay]];
  140.         [theScrollView setDocView:theSoundView];
  141.  
  142.         [[theScrollView superview] setAutoresizeSubviews:YES];
  143.         [[theScrollView superview] setAutosizing:
  144.                             NX_HEIGHTSIZABLE|NX_WIDTHSIZABLE];
  145.     
  146.         [TheSoundTable NewItem:theSound:theSoundView:theWindow:nameoffile:NO];
  147.  
  148.         [theWindow setTitleAsFilename:nameoffile];
  149.         [theWindow setMiniwindowIcon: "SoundIcon"];
  150.         [theWindow display];
  151.         [theWindow setDelegate:self];
  152.         [theWindow makeKeyAndOrderFront:self];
  153.         [theSoundView setDelegate:TheSoundManager];
  154.         [theSoundView setContinuous: [ThePreferencesManager ContinuousSelection]];
  155.             // For Continuous Sample Update
  156.         [theSound setDelegate:TheSoundManager];
  157.         [self TurnOnMenu:self];
  158.         }
  159.     return self;
  160. }
  161.  
  162. - Close:sender
  163. {
  164.     if (!([self CurrentWindow:self]==NULL))
  165.         {[[self CurrentWindow:self] performClose:self];}
  166.     return self;
  167. }
  168.  
  169. - Quit:sender
  170. {
  171.        Quitting=YES;
  172.     [ThePreferencesManager SavePreferences];
  173.     if ([TheSoundTable FindChangedSound])
  174.         {
  175.         DoQuit=NO;
  176.         CurrentPanel=QuitPanel;
  177.         [QuitPanel makeKeyAndOrderFront:self];
  178.         [NXApp runModalFor:QuitPanel];
  179.         if (DoQuit==YES) {[NXApp terminate:self];}
  180.         Quitting=NO;
  181.         }
  182.     else [NXApp terminate:self];
  183.     return self;
  184. }
  185.  
  186. - Revert:sender
  187. {
  188.     char nameoffile[MAXSTRINGLENGTH];
  189.     [TheSoundTable FindSound:[self CurrentSound:self]];
  190.     strcpy (nameoffile, [TheSoundTable ReturnFileName]);
  191.     [[self CurrentSound:self] initFromSoundfile: nameoffile];
  192.     return self;
  193. }
  194.  
  195. - SaveAll:sender
  196. {
  197.     BOOL DoSave=YES;
  198.     BOOL SoundsLeft=YES;
  199.     while (SoundsLeft)
  200.         {
  201.         SoundsLeft=[TheSoundTable FindChangedSound];
  202.         if (SoundsLeft)
  203.             {
  204.             [[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
  205.             DoSave=([self Save]);
  206.             if (!DoSave) 
  207.                 {
  208.                 SoundsLeft=NO;
  209.                 }
  210.             }
  211.  
  212.         }
  213.     return self;
  214. }
  215.  
  216. - SaveAs:sender
  217. {
  218.     [self SaveAs];
  219.     return self;
  220. }
  221.  
  222. - Save:sender
  223. {
  224.     [self Save];
  225.     return self;
  226. }
  227.  
  228. - (BOOL) SaveAs
  229. {
  230.     id savepanel;
  231.     char* type="snd";
  232.     char nameoffile[MAXSTRINGLENGTH];
  233.     
  234.     PanelCancelled=YES;
  235.     [TheSoundTable FindWindow:[self CurrentWindow:self]];
  236.     savepanel=[SavePanel new];
  237.     [savepanel setDelegate:self];
  238.     [savepanel setRequiredFileType:type];
  239.     CurrentPanel=savepanel;
  240.     [savepanel runModal];
  241.     strcpy (nameoffile, [savepanel filename]);
  242.     if (!PanelCancelled)
  243.         {
  244.             [[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
  245.             [[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
  246.             [[TheSoundTable ReturnWindow] setDocEdited:NO];
  247.             [TheSoundTable SetFileName:nameoffile];
  248.             [TheSoundTable SoundReset];
  249.             return YES;
  250.         }
  251.     else
  252.         {
  253.             return NO;
  254.         }
  255. }
  256.  
  257. - (BOOL) Save
  258. {
  259.     char nameoffile[MAXSTRINGLENGTH];
  260.     BOOL Untitled=[TheSoundTable ReturnUntitled];
  261.     int SoundDataLength;
  262.     [TheSoundTable FindWindow:[self CurrentWindow:self]];
  263.     SoundDataLength=[[TheSoundTable ReturnSound] dataSize];
  264.     strcpy (nameoffile, [TheSoundTable ReturnFileName]);
  265.     if (SoundDataLength)
  266.         {
  267.         if (!Untitled)
  268.             {
  269.             [TheSoundTable FindSound:[self CurrentSound:self]];
  270.             [[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
  271.             [[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
  272.             [[TheSoundTable ReturnWindow] setDocEdited:NO];
  273.             [TheSoundTable SetFileName:nameoffile];
  274.             [TheSoundTable SoundReset];
  275.             return YES;
  276.             }
  277.         else
  278.             {
  279.             return [self SaveAs];
  280.             }
  281.         }
  282.     else 
  283.         {
  284.         [NXApp runModalFor:NoDataSavePanel];
  285.         [[TheSoundTable ReturnWindow] setDocEdited:NO];
  286.         [TheSoundTable SoundReset];
  287.         return YES;
  288.         }
  289. }
  290.  
  291. - Print:sender
  292. {
  293.     if (!([self CurrentWindow:self]==NULL))
  294.         {
  295.         [TheSoundTable FindWindow:[self CurrentWindow:self]];
  296.         [[TheSoundTable ReturnSoundView] printPSCode:self];
  297.         }
  298.     return self;
  299. }
  300.  
  301. - CurrentWindow:sender
  302. {
  303.     id thestuff=[NXApp mainWindow];
  304.     return thestuff;
  305. }
  306.  
  307. - CurrentSound:sender
  308. {
  309.     if ([self CurrentWindow:self]==NULL) {return NULL;}
  310.     else {
  311.           [TheSoundTable FindWindow:[self CurrentWindow:self]];
  312.           return [TheSoundTable ReturnSound];
  313.          }
  314. }
  315.  
  316. - CurrentSoundView:sender
  317. {
  318.     if ([self CurrentWindow:self]==NULL) {return NULL;}
  319.     else {
  320.           [TheSoundTable FindWindow:[self CurrentWindow:self]];
  321.           return [TheSoundTable ReturnSoundView];
  322.          }
  323. }
  324.  
  325. - SoundChanged:ThisSound
  326. {
  327.        float TheReduction;
  328.     [TheSoundTable FindSound:ThisSound];
  329.     TheReduction=[[TheSoundTable ReturnSoundView] reductionFactor];
  330.     [TheSoundTable SoundEdited];
  331.     [[TheSoundTable ReturnWindow] setDocEdited:YES];
  332.     [[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
  333.     [[TheSoundTable ReturnSoundView] setSound:ThisSound];
  334.     [[TheSoundTable ReturnSoundView] setReductionFactor: TheReduction];
  335.     [TheEditController ChangeSelection:self];
  336.     [TheEditController ChangeView:self];
  337.     [TheEditController ZoomChanged:self];
  338.     [TheInfoManager ChangeInfo: ThisSound];
  339.     return self;
  340. }
  341.  
  342.  
  343.          /*----------------*USED INTERNALLY*-------------------*/
  344.  
  345. - SaveBeforeQuit:sender;
  346. {
  347.     BOOL SoundsLeft=YES;
  348.     [NXApp stopModal];
  349.     [QuitPanel close];
  350.     while (SoundsLeft)
  351.         {
  352.         SoundsLeft=[TheSoundTable FindChangedSound];
  353.         if (SoundsLeft)
  354.             {
  355.             [[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
  356.             DoQuit=([self Save]);
  357.             if (!DoQuit) 
  358.                 {
  359.                 SoundsLeft=0;
  360.                 }
  361.             }
  362.         }
  363.     return self;
  364. }
  365.  
  366. - DontSaveBeforeQuit:sender;
  367. {
  368.     [NXApp stopModal];
  369.     [QuitPanel close];
  370.     DoQuit=YES;
  371.     return self;
  372. }
  373.  
  374. - CancelSaveBeforeQuit:sender;
  375. {
  376.     [NXApp stopModal];
  377.     [QuitPanel close];
  378.     DoQuit=NO;
  379.     return self;
  380. }
  381.  
  382. - SaveBeforeClose:sender
  383. {
  384.     id savepanel;
  385.     char* type="snd";
  386.     char nameoffile[MAXSTRINGLENGTH];
  387.     BOOL Untitled=[TheSoundTable ReturnUntitled];
  388.     int SoundDataLength;
  389.     [NXApp stopModal];
  390.     [ClosePanel close];
  391.     
  392.     [TheSoundTable FindWindow:[self CurrentWindow:self]];
  393.     SoundDataLength=[[TheSoundTable ReturnSound] dataSize];
  394.     strcpy (nameoffile, [TheSoundTable ReturnFileName]);
  395.     if (SoundDataLength)
  396.         {
  397.         if (!Untitled)
  398.             {
  399.             [[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
  400.             [[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
  401.             [[TheSoundTable ReturnWindow] setDocEdited:NO];
  402.             [TheSoundTable SetFileName:nameoffile];
  403.             [TheSoundTable SoundReset];
  404.             DoClose=YES;
  405.             }
  406.         else
  407.             {
  408.             PanelCancelled=YES;
  409.             savepanel=[SavePanel new];
  410.             [savepanel setDelegate:self];
  411.             [savepanel setRequiredFileType:type];
  412.             CurrentPanel=savepanel;
  413.             [savepanel runModal];
  414.             strcpy (nameoffile, [savepanel filename]);
  415.             if (!PanelCancelled)
  416.                 {
  417.                     [[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
  418.                     [[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
  419.                     [[TheSoundTable ReturnWindow] setDocEdited:NO];
  420.                     [TheSoundTable SetFileName:nameoffile];
  421.                     [TheSoundTable SoundReset];
  422.                     DoClose=YES;
  423.                 }
  424.             else DoClose=NO;
  425.             }
  426.         }
  427.         else
  428.         {
  429.         [NXApp runModalFor:NoDataSavePanel];
  430.         [[TheSoundTable ReturnWindow] setDocEdited:NO];
  431.         [TheSoundTable SoundReset];
  432.         DoClose=YES;
  433.         }
  434.     return self;
  435. }
  436.  
  437. - DontSaveBeforeClose:sender
  438. {
  439.     [NXApp stopModal];
  440.     [ClosePanel close];
  441.     
  442.     DoClose=YES;
  443.     return self;
  444. }
  445.  
  446. - CancelSaveBeforeClose:sender
  447. {
  448.     [NXApp stopModal];
  449.     [ClosePanel close];
  450.     
  451.     DoClose=NO;
  452.     return self;
  453. }
  454.  
  455. - TurnOnMenu:sender
  456. {
  457.     [SaveButton setEnabled:YES];
  458.     [SaveAsButton setEnabled:YES];
  459.     [SaveAllButton setEnabled:YES];
  460.     [RevertButton setEnabled:YES];
  461.     [CloseButton setEnabled:YES];
  462.     [TheModuleController TurnOnMenu:self];
  463.     [TheEditController TurnOnMenu:self];
  464.     return self;
  465. }
  466.  
  467. - TurnOffMenu:sender
  468. {
  469.     [SaveButton setEnabled:NO];
  470.     [SaveAsButton setEnabled:NO];
  471.     [SaveAllButton setEnabled:NO];
  472.     [RevertButton setEnabled:NO];
  473.     [CloseButton setEnabled:NO];
  474.     [TheModuleController TurnOffMenu:self];
  475.     [TheEditController TurnOffMenu:self];
  476.     return self;
  477. }
  478.  
  479.  
  480. -  CheckInformation:sender;
  481. {
  482.     if ([self CurrentSound:self]==NULL)
  483.         {
  484.         [self TurnOffInformation:self];
  485.         }
  486.     else
  487.         {
  488.         [self TurnOnInformation:self];
  489.         }
  490.     return self;
  491. }
  492.  
  493.  
  494. - TurnOffInformation:sender;
  495. {
  496.     [TheInfoManager NoWindow];
  497.     [TheEditController ResetSelection:self];
  498.     return self;
  499. }
  500.  
  501.  
  502.  
  503. - TurnOnInformation:sender;
  504. {
  505.     [TheInfoManager ChangeInfo: [self CurrentSound:self]];
  506.     [TheEditController ChangeSelection:self];
  507.     [TheEditController ChangeView:self];
  508.     return self;
  509. }
  510.  
  511.  
  512.  
  513. - NoDataSaveOkay:sender
  514. {
  515.     [NXApp stopModal];
  516.     [NoDataSavePanel close];
  517.     return self;
  518. }
  519.  
  520.  
  521. - DoPageLayout:sender
  522. {
  523.     [NXApp runPageLayout:self];
  524.     return self;
  525. }
  526.  
  527.  
  528. /*IMPLEMENTED AS A WINDOW DELEGATE*/
  529.  
  530.  
  531. - windowDidResize:sender
  532. {
  533.     NXSize ThisSize;
  534.     NXRect ThisRect;
  535.     [TheSoundTable FindWindow:sender];
  536.     
  537.     [[[TheSoundTable ReturnWindow] contentView] getContentSize: &ThisSize];
  538.     [[TheSoundTable ReturnSoundView] getBounds:&ThisRect];
  539.     
  540.     [[TheSoundTable ReturnSoundView] sizeTo: ThisRect.size.width : ThisSize.height];
  541.                 
  542.     [TheEditController ZoomChanged:self];
  543.     return self;
  544. }
  545.  
  546. - windowDidBecomeKey:sender
  547. {
  548.     if (!(sender==CurrentPanel))
  549.         {
  550.         [Console makeKeyWindow];
  551.         [TheSoundTable FindWindow:sender];
  552.         [TheInfoManager ChangeInfo:[TheSoundTable ReturnSound]];
  553.         [TheEditController ZoomChanged:self];
  554.         }
  555.     [TheEditController ChangeSelection:self];
  556.     [TheEditController ChangeView:self];
  557.     return self;
  558. }
  559.  
  560. - windowDidBecomeMain:sender
  561. {
  562.     [TheEditController ChangeSelection:self];
  563.     [TheEditController ChangeView:self];
  564.     return self;
  565. }
  566.  
  567. - windowWillClose:sender
  568. {
  569.  
  570.     id TheWindow;
  571.     if (!Quitting)
  572.         {
  573.         [sender becomeMainWindow];
  574.         [sender orderFront:self];
  575.         [TheSoundTable FindWindow:[self CurrentWindow:self]];
  576.         TheWindow=[TheSoundTable ReturnWindow];
  577.         if ([TheSoundTable IsSoundChanged])
  578.             {
  579.             [ClosePanel makeKeyAndOrderFront:self];
  580.             [NXApp runModalFor:ClosePanel];
  581.             if (DoClose==YES)
  582.                 {
  583.                 [TheSoundTable FindWindow:TheWindow];
  584.                 [[TheSoundTable ReturnSound] free];
  585.                 [TheSoundTable Delete];
  586.                 }
  587.             else return NULL;
  588.             }
  589.         else
  590.             {
  591.             [TheSoundTable FindWindow:TheWindow];
  592.             [[TheSoundTable ReturnSound] free];
  593.             [TheSoundTable Delete];
  594.             }
  595.         printf("SoundTable number:  %d", [TheSoundTable Total]);
  596.         if (!([TheSoundTable Total]-1))
  597.             {
  598.             [self TurnOffMenu:self];
  599.             }
  600.         }
  601.     return self;
  602. }
  603.  
  604. - WindowDidUpdate:sender
  605. {
  606.     [TheEditController ChangeSelection:self];
  607.     [TheEditController ChangeView:self];
  608.     return self;
  609. }
  610.  
  611. - WindowDidResize:sender
  612. {
  613.     [TheEditController ChangeView:self];
  614.     return self;
  615. }
  616.  
  617.  
  618. /* IMPLEMENTED AS A DELEGATE OF SAVEPANEL AND OPENPANEL */
  619.  
  620. - (BOOL) panelValidateFilenames:sender
  621. {
  622.     PanelCancelled=NO;
  623.     return YES;
  624. }
  625.  
  626.  
  627. /* IMPLEMENTED AS A DELEGATE OF APPLICATION */
  628.  
  629.  
  630. - appDidInit:sender
  631. {
  632.     [Console setFloatingPanel:YES];
  633.     [Console makeKeyWindow];
  634.     return self;
  635. }
  636.  
  637.  
  638. - appDidBecomeActive:sender
  639. {
  640.     [Console makeKeyWindow];
  641.     return self;
  642. }
  643.  
  644.  
  645. - appDidUnhide:sender
  646. {
  647.     [Console makeKeyWindow];
  648.     return self;
  649. }
  650.  
  651.  
  652. - appDidUpdate:sender
  653. {
  654.     [Console makeKeyWindow];
  655.     return self;
  656. }
  657.  
  658.  
  659. @end
  660.